package janino;

import org.codehaus.janino.ClassBodyEvaluator;
public class DynamicCodeGenerationExample {
    public static void main(String[] args) throws Exception {
        // Create an instance of ClassBodyEvaluator
        ClassBodyEvaluator cbe = new ClassBodyEvaluator();
        // Define the dynamic class contents
        String classContents = "public class DynamicClass {" +
                "public void printMessage() {" +
                "System.out.println(\"Dynamically generated code\");" +
                "}" +
                "}";
        // Compile the dynamic class
        cbe.cook(classContents);
        // Instantiate the dynamic class
        Object instance = cbe.getClazz().getConstructor().newInstance();
        // Invoke a method on the dynamic class
        instance.getClass().getMethod("printMessage").invoke(instance);
    }
}